import React from 'react';
import { Tooltip, Modal, Button } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { Link, } from 'umi';
// import ComIcon from '../ComIcon';
import styles from './index.less';
import { isAuth } from '@/utils/common';

const ICON_TYPE = {
  del: 'del',  // 删除
  edit: 'edit', // 编辑
  enable: 'enable', // 启用停用
  disabled: 'disabled',
  pwd: 'password', // 修改密码
  setting: 'setting', // 设置
  view: 'view',// 预览
  review: 'review',// 查看
  copy: 'copy',
  ground: 'ground', //上架
  undercarriage: 'undercarriage',// 下架
  recharge: 'recharge',// 充值
  credit: 'credit',// 授信
  audit: 'audit',// 审核
}

const TableBtns = ({ buttons = [] }) => (

  buttons.map(el => {
    const {
      auth, // 按钮权限
      icon = "CSV", // icon类型
      name, // 按钮名称
      type, // 类型 link,confirm
      method = () => { }, // 回调方法
      pathname, // 如果type是link,配置路径
      confirmText = '是否确认操作？', // 如果type是confirm,配置文案
    } = el;

    let hasAuth = true;
    // if (typeof auth === 'boolean') {
    //   hasAuth = auth;
    // } else if (typeof auth === 'string' || Array.isArray(auth)) {
    //   hasAuth = isAuth(auth);
    // }

    if (hasAuth) {
      let html = <Button type="link" onClick={() => {
        if (type === "confirm") {
          Modal.confirm({
            icon: <ExclamationCircleOutlined />,
            title: '温馨提示',
            content: confirmText,
            okText: '确认',
            cancelText: '取消',
            onOk() {
              method();
            },
          });
        } else {
          method();
        }
      }}>{name}</Button>
      if (type === 'link') { // 链接跳转
        html = (
          <Link to={pathname} > {name}</Link>
        )
      }
      if (type === 'button') {
        html = (
          <Button type="link" onClick={method}>{name}</Button>
        )
      }
      return (
        <Tooltip title={name} key={name}>
          {html}
        </Tooltip>
      )
    }
  })
);

TableBtns.defaultProps = {
  buttons: [], // 按钮数组
};

TableBtns.ICON_TYPE = ICON_TYPE;

export default TableBtns;
